package com.dailyblue.java.thread;

import java.util.concurrent.TimeUnit;

public class DemoH {
    public static void main(String[] args) throws Exception {
        Object lock1 = new Object();
        Object lock2 = new Object();
        Thread t1 = new Thread(() -> {

            synchronized (lock1) {
                System.out.println("A获取到了锁A");
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (lock2) {
                    System.out.println("A获取到了锁B");
                }
            }


        });
        t1.start();
        Thread t2 = new Thread(() -> {
            synchronized (lock2) {
                System.out.println("B获取到了锁B");
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (lock1) {
                    System.out.println("B获取到了锁A");
                }
            }

        });
        t2.start();
    }
}